package com.hksj.Intelligentmanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.Intelligentmanagement.common.Result;
import com.hksj.Intelligentmanagement.dto.CallDeviceScreenDTO;
import com.hksj.Intelligentmanagement.entity.CallDeviceScreenEntity;
import com.hksj.Intelligentmanagement.mapper.CallDeviceScreenMapper;
import com.hksj.Intelligentmanagement.vo.CallDeviceScreenVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CallDeviceScreenService {
    @Autowired
    private CallDeviceScreenMapper deviceScreenMapper;

    //新增呼叫设备信息
    @Transactional(propagation = Propagation.REQUIRES_NEW)//异常处理
    public Result createCallDeviceScreen(CallDeviceScreenDTO deviceScreenDTO) {
        QueryWrapper<CallDeviceScreenEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("name", deviceScreenDTO.getName());
        Integer count = deviceScreenMapper.selectCount(wrapper);
        if (count > 0) {
            return Result.error("呼叫设备名称已存在");
        }
        QueryWrapper<CallDeviceScreenEntity> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("code", deviceScreenDTO.getCode());
        Integer count1 = deviceScreenMapper.selectCount(wrapper1);
        if (count1 > 0) {
            return Result.error("呼叫设备编码已存在");
        }
        QueryWrapper<CallDeviceScreenEntity> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("operating_room_no", deviceScreenDTO.getOperatingRoomNo());
        Integer count2 = deviceScreenMapper.selectCount(wrapper2);
        if (count2 > 0) {
            return Result.error("手术室编号已存在");
        }
        CallDeviceScreenEntity callDeviceScreenEntity = new CallDeviceScreenEntity();
        BeanUtils.copyProperties(deviceScreenDTO, callDeviceScreenEntity);
        //在表中创建一条记录
        int insert = deviceScreenMapper.insert(callDeviceScreenEntity);
        if (insert <= 0) {
            return Result.error("新增失败");
        }
        return Result.ok("新增成功");
    }

    //删除呼叫设备信息
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result deleteCallDeviceScreen(String id) {
        int insert = deviceScreenMapper.deleteById(id);
        if (insert <= 0) {
            return Result.error("删除失败");
        }
        return Result.ok("删除成功");
    }

    //更改呼叫设备信息
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result updateCallDeviceScreen(CallDeviceScreenDTO callEquipmentDTO) {
        CallDeviceScreenEntity callDeviceScreenEntity = new CallDeviceScreenEntity();
        BeanUtils.copyProperties(callEquipmentDTO, callDeviceScreenEntity);
        int i = deviceScreenMapper.updateById(callDeviceScreenEntity);
        if (i <= 0) {
            return Result.error("更新失败");
        }
        return Result.ok("更新成功");
    }

    //根据id查询呼叫设备信息
    public Result queryCallDeviceScreenById(String id) {
        //调用selectById查询专业信息
        CallDeviceScreenEntity entity = deviceScreenMapper.selectById(id);
        //返回查询结果
        return Result.data(entity);
    }

    //模糊查询呼叫设备信息
    public Page<CallDeviceScreenVo> queryCallDeviceScreenListByPage(CallDeviceScreenDTO deviceScreenDTO) {
        //创建一个分页实例 mybatis plus的Page类型为IPage子类 能实现 对查询结果集排序 也能直接返回
        Page<CallDeviceScreenVo> page = new Page<>(deviceScreenDTO.getPageNum(), deviceScreenDTO.getPageSize());
        //在SQL查询结果集使用创建时间正序排序
        page.addOrder(OrderItem.asc("create_time"));
        //对查询进行分页
        List<CallDeviceScreenVo> cellEquipmentVos = deviceScreenMapper.queryCallDeviceScreenListByPage(page, deviceScreenDTO);
        //保存分页记录
        page.setRecords(cellEquipmentVos);
        //返回分页
        return page;
    }

    public CallDeviceScreenVo getByOperatingRoomNo(String operatingRoomNo){
        return deviceScreenMapper.getByOperatingRoomNo(operatingRoomNo);
    }
}
